<template>
    <div>
      <el-dialog title="合并付款" :visible.sync="isShow" width="20%" append-to-body>
        <el-form
          :model="formData"
          :inline="true"
          ref="formDataRef"
          :rules="rules"
          label-width="auto"
          size="mini"
        >
          <el-row>
            <el-form-item label="记账日期" prop="accountDate">
              <el-date-picker
                v-model="formData.accountDate"
                type="date"
                value-format="yyyy-MM-dd"
                style="width: 190px"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-row>

          <el-row>
            <el-form-item label="银行账号" prop="bankAccount">
              <el-select
                v-model="formData.bankAccount"
                filterable
                clearable
                placeholder="请选择"
                style="width: 190px"
              >
                <el-option
                  v-for="(item, key) in companyBankMap"
                  :key="key"
                  :label="item"
                  :value="parseInt(key)">
                </el-option>
              </el-select>
            </el-form-item>
          </el-row>

          <el-row>
            <el-form-item label="金额" prop="totalAmount">
              <el-input-number
                v-model="formData.totalAmount"
                :controls="false"
                :precision="2"
                :disabled="true"
                style="width: 190px">
              </el-input-number>
            </el-form-item>
          </el-row>

          <el-button size="small" @click="cancel" style="margin-left: 100px">取消</el-button>
          <el-button type="primary" size="small" @click="save">确定</el-button>
        </el-form>
      </el-dialog>
    </div>
</template>

<script>

  import { ajax } from "@/store/yg/common/utils";

    export default {
        data() {
            return {
              formData: {},
              financeAccountNos: [],
              isShow: false,
              companyBankMap: {},
              rules: {
                accountDate: [{
                  required: true,
                  message: '请选择',
                  trigger: 'blur'
                }],
                bankAccount: [{
                  required: true,
                  message: '请选择',
                  trigger: 'blur'
                }],
                totalAmount: [{
                  required: true,
                  message: '请输入',
                  trigger: 'blur'
                }]
              },
            };
        },

        methods: {
          check() {
            //获取选中的行
            const selectedData = this.$store.state.main.selectedData;
            if (selectedData.length < 2) {
              this.$message.error("请至少勾选2条记录！");
              return;
            }
            let allServeCompanyIdMap = new Map();
            let allCurrencyMap = new Map();
            let totalAmount = 0;

            //校验
            for (let i = 0; i < selectedData.length; i++) {
              let item = selectedData[i];
              //accountType 财务类型  0=应收，1=应付
              if (item.accountTypeId == 0) {
                this.$message.error("只能勾选财务类型为应付的记录！");
                return;
              }
              if (item.hasVoucherNo) {
                let msg = "记账编号：" + item.accountNo + "，已生成凭证，不允许合并！";
                this.$message.error(msg);
                return;
              }
              allServeCompanyIdMap.set(item.serOrCusId, item.serOrCusId);
              allCurrencyMap.set(item.currency, item.currency);
              totalAmount += item.amount;
              this.financeAccountNos.push(item.accountNo);
            }
            if (allServeCompanyIdMap.size > 1) {
              this.$message.error("必须是同一服务商！");
              return;
            }
            if (allCurrencyMap.size > 1) {
              this.$message.error("必须是同一币种！");
              return;
            }
            this.formData.totalAmount = totalAmount.toFixed(2);
            //弹出框
            this.isShow = true;
          },

          //查询初始化数据
          async init() {
            const { code, data, message } = await ajax("getMergePayInitData", {});
            if (code !== 200) {
              if (message) {
                this.$message.error(message);
              } else {
                this.$message.error("服务器异常");
              }
              return;
            }
            this.companyBankMap = data.companyBankMap;
          },

          /**
           * 点击确定，跳转至凭证编辑页
           * */
          async save() {
            this.isShow = false;
            let url = "#/blank/finance__voucher__merge?financeAccountNoJoinStr="
              + this.financeAccountNos.toString()
              + "&accountDate=" + this.formData.accountDate
              + "&bankAccountId=" + this.formData.bankAccount;

            window.open(url, "_blank");
          },

          cancel() {
            this.isShow = false;
          }
        },

        mounted() {
          this.init();
          this.check();
        }
    };
</script>


<style>

</style>
